package com.tamartech.greendb.canvass;

import java.io.File;
import java.io.FileOutputStream;
import java.util.LinkedList;
import java.util.List;

import com.itextpdf.text.Document;
import com.itextpdf.text.pdf.PdfWriter;
import com.tamartech.greendb.dao.ElectoralRollDAO;
import com.tamartech.greendb.dao.ElectoralRollEntryDAO;
import com.tamartech.greendb.dao.WardDAO;
import com.tamartech.greendb.data.Address;
import com.tamartech.greendb.data.ElectoralRoll;
import com.tamartech.greendb.data.ElectoralRollEntry;
import com.tamartech.greendb.data.Ward;

public class Test
{

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception
    {
        String wardName = "REDHILLEAST";
        File file = new File ("/dump/testCanvass.pdf");
        String street = "LYNWOOD ROAD";
        
        WardDAO wDAO = new WardDAO();
        Ward w = wDAO.get(wardName);
        
        if (w == null)
            throw new Exception ("Ward " + wardName + " not found");
        
        ElectoralRollDAO erDAO = new ElectoralRollDAO();        
        ElectoralRoll roll = erDAO.getLatestByWard(w);
        
        if (roll == null)
            throw new Exception ("Latest electoral roll for ward: " + wardName + " not found");
        
        ElectoralRollEntryDAO ereDAO = new ElectoralRollEntryDAO();        
        List<ElectoralRollEntry> allEntries = ereDAO.listByRoll(roll);
        
        List<ElectoralRollEntry> entries = new LinkedList<ElectoralRollEntry>();
        
        for (ElectoralRollEntry e : allEntries)
        {
            Address a = e.getAddress();
            
            if (a.getAddress1().contains(street) || a.getAddress2().contains(street))
                entries.add(e);
        }
        
        Document document = new Document();

        // we create a writer that listens to the document
        // and directs a PDF-stream to a file
    
        PdfWriter.getInstance(document, new FileOutputStream(file));

        document.open();
        
        CanvassSheetWriter writer = new CanvassSheetWriter (entries, document);
        writer.write();
        
        document.close();
    }

}
